class TruckOwnershipsController < ApplicationController
  def new
    @truck_ownership = TruckOwnership.new
    @truck = Truck.find(params[:truck_id])
  end

  def create
    @truck = Truck.find(params[:truck_id])

    from_date = Date.strptime(params[:truck_ownership][:from_date], "%d-%m-%Y")
    through_date = Date.strptime(params[:truck_ownership][:through_date], "%d-%m-%Y")

    params[:truck_ownership][:from_date] = from_date
    params[:truck_ownership][:through_date] = through_date

    truck_ownership = TruckOwnership.new(params[:truck_ownership])

    truck_ownership.truck_id = params[:truck_id]
    
    respond_to do |format|
      if truck_ownership.save
        format.html  { redirect_to(ownerships_truck_path(@truck),
                      :notice => 'Truck ownership was added successfully for the truck.') }
      else
        format.html  { render :action => "new" }
      end
    end
  end
end
